package com.hp.bon.sgw.threads;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

import com.hp.bon.security.config.KeyManager;
import com.hp.bon.sgw.util.dcc.DccAvpHelper;

class WarningTree extends AnanlysBase {
	public WarningTree() {
		super("BON");
	}

	private ConcurrentHashMap<String, Leaf4SCID> leafSCIDMap = new ConcurrentHashMap<String, Leaf4SCID>();
	private HashMap<String, Sub4Domain> treeMap = new HashMap<String, Sub4Domain>();

	public void calcStatus() {
		super.init();
		for (Sub4Domain sub4Domain : treeMap.values()) {
			sub4Domain.calcStatus();
			counterByStatus(sub4Domain.curStatus);
		}
		super.calcStatus();
	}

	@Override
	protected void collectChildWarning(ArrayList<String> warningList, long begin) {
		for (Sub4Domain sub4Domain : treeMap.values()) {
			sub4Domain.collectWaring(warningList, begin);
		}
	}

	public void addLeaf(Leaf4SCID leave4scid) {
		String scid = leave4scid.name;
		leafSCIDMap.put(scid, leave4scid);

		String area = DccAvpHelper.getArea(scid);
		if (area.endsWith("001")) {
			return;
		}
		Sub4Domain sub4Domain = treeMap.get(area);
		if (sub4Domain == null) {
			sub4Domain = new Sub4Domain(area);
			treeMap.put(area, sub4Domain);
		}
		String neName = KeyManager.getInstance().getNeCategoryAndDomainFromSCID(scid);
		Sub4NE sub4NE = sub4Domain.neMap.get(neName);
		if (sub4NE == null) {
			sub4NE = new Sub4NE(neName);
			sub4Domain.neMap.put(neName, sub4NE);
		}
		Leaf4SCID leave4SCID1 = sub4NE.scidMap.get(scid);
		if (leave4SCID1 == null) {
			leave4SCID1 = leave4scid;
			sub4NE.scidMap.put(scid, leave4scid);
		}
	}

	@Override
	protected String getType() {
		return "ALL ";
	}

	public Collection<Leaf4SCID> allLeaf() {
		return leafSCIDMap.values();
	}

	public Leaf4SCID getLeaf(String _contextId) {
		return leafSCIDMap.get(_contextId);
	}
}
